package com.egao.common.module.AI.Utils;

import java.util.Arrays;

public class VectorUtils {// 方法：计算两个向量的点积
    public static double dotProduct(double[] a, double[] b) {
        double result = 0.0;
        for (int i = 0; i < a.length; i++) {
            result += a[i] * b[i];
        }
        return result;
    }

    // 方法：计算向量的模
    public static double norm(double[] a) {
        double sum = 0.0;
        for (double value : a) {
            sum += value * value;
        }
        return Math.sqrt(sum);
    }

    // 方法：计算余弦相似度
    public static double cosineSimilarity(double[] a, double[] b) {
        return dotProduct(a, b) / (norm(a) * norm(b));
    }

    // 方法：拼接两个数组
    public static double[] concatenate(double[] a, double[] b) {
        double[] result = new double[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static void main(String[] args) {
        // 示例数据
        double[] majorIndex = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7};
        double[] abilityIndex = {0.8, 0.9, 1.0, 1.1, 1.2, 1.3};
        int yxdwxz = 1;
        int yxdwhy = 2;

        // 将 yxdwxz 和 yxdwhy 转换为数组
        double[] yxdwxzArray = {yxdwxz};
        double[] yxdwhyArray = {yxdwhy};

        // 拼接所有数组
        double[] vector = concatenate(concatenate(majorIndex, abilityIndex), concatenate(yxdwxzArray, yxdwhyArray));

        // 构造一个全1向量
        double[] onesVector = new double[15];



        // 计算余弦相似度
        double similarity = cosineSimilarity(vector, onesVector);

        // 输出结果
        System.out.println("Vector: " + Arrays.toString(vector));
        System.out.println("Cosine Similarity with Ones Vector: " + similarity);
    }
}